package com.shop.user.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.core.HttpMessageResult;
import com.shop.core.PageForm;
import com.shop.entity.ApUser;
import com.shop.user.common.UserInfoEnum;
import com.shop.user.dao.ApUserMapper;

@Service
public class ApUserService {

	@Autowired
	private ApUserMapper apUserMapper;
	
	@Autowired
	private BCryptPasswordEncoder encoder;

	public ApUser getUserByUsername(String username) {
		return apUserMapper.getUserByUsername(username);
	}
	
	public ApUser getUserById(String userId) {
		return apUserMapper.selectByPrimaryKey(userId);
	}

	@Transactional
	public HttpMessageResult insertUser(ApUser user) {
		return HttpMessageResult.doHttp(UserInfoEnum.USER_OPER_SUCCESS.getCode(), UserInfoEnum.USER_OPER_SUCCESS.getMessage(), apUserMapper.insert(user));
	}

	public PageInfo<ApUser> getUserPageByCond(PageForm pageForm) {
		PageHelper.startPage(pageForm.getPage(), pageForm.getRows());
		return apUserMapper.getUserPageByCond(pageForm.getCondition());
	}
	
	public ApUser doUserLogin(String username, String password) {
		ApUser user = apUserMapper.getUserByUsername(username);
		if(user!=null && encoder.matches(password, user.getPassword())){
			return user;
		}
		return null;
	}

	

}
